package leetcode;

import java.util.Arrays;
import java.util.List;

public class Solution2 {
    public static  int searchInsert(int[] nums, int target) {
        int index =nums.length;
        for (int i = 0; i < nums.length; i++) {
			if(nums[i]>=target){
				index=i;
				break;
			}
		}
        return index;
    }
    public static void reverseString(char[] s) {
    	if(s.length>=2){
    		char s1[]=new char[s.length-1];
    		//如何去掉最后那个
    		for (int i = 0; i < s.length-1; i++) {
    			s1[i]=s[i];
    		}
	    	//每次输出length-1那个
    
	        System.out.print(s[s.length-1]);
	        reverseString(s1);
    	}else if(s.length==1){
    		System.out.print(s[0]);
    		return ;
    	}else {
    		return;
    	}
    }
    
   
    //翻转图像
    public static int[][] flipAndInvertImage(int[][] A) {
    	//先水平翻转
    	//反转图像 0变为1  1变为0
    	 for (int i = 0; i < A.length; i++) {
    		 for (int j = 0,x=A[i].length-1; j < A[i].length&&x>j; j++,x--) {
					System.out.println(A[i][x]+"<-->"+A[i][j]);
					int temp=A[i][x];
					A[i][x]=A[i][j];
					A[i][j]=temp;
			}
 			for (int j = 0; j < A[i].length; j++) {
 				
						
				if(A[i][j]==0) {
					A[i][j]=1;
				}else {
					A[i][j]=0;
				}
			}
		}
        return A;
    }
    
    //删除排序数组中的重复个数为2元素
    public static int removeDuplicates(int[] nums) {
    	int count=0;//定义标记
    	//0,1,1,2,2,2,2,2,3,4
     	for (int i = 0; i < nums.length-count; i++) {
			//如果当前比较的数 与  他的下标+2的数 一样 则需要 其下表加二往后的数依次往前移
     		while(i+2<nums.length-count&&nums[i]==nums[i+2]) {
     			count++;
     			//从i+2往后的每一个元素往前移
     			for (int j = i+3; j < nums.length; j++) {
					nums[j-1]= nums[j];
				}
     		}
		}
     	
     	return nums.length-count;
    }
    public static void main(String[] args) {
		
    	//搜索插入的位置
//    	int[] nums={1,3,4,6};
//    	int target=7;
//    	System.out.println(searchInsert(nums, target));
    	
    	//字符串翻转
//    	char[] s ="hello".toCharArray();
//    	reverseString(s);
    	
    	int []nums= {0,0,1,1,1,1,2,3,3};
    	int removeDuplicates = removeDuplicates(nums);
    	System.out.println(removeDuplicates);
    	for (int i : nums) {
			System.out.print(i);
		}
    	
//    	int[][]A= {{1,1,0},{1,0,1},{0,0,0}};
//    	int[][] flipAndInvertImage = flipAndInvertImage(A);
//    	for (int i = 0; i < flipAndInvertImage.length; i++) {
//			for (int j = 0; j < flipAndInvertImage[i].length; j++) {
//				System.out.print(flipAndInvertImage[i][j]+",");
//			}
//			System.out.println();
//		}
	}
}